package com.white.commonlib.adapter.listgrid;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.widget.ImageView;

import com.white.commonlib.engine.img.DefaultImageLoader;

/**
 * Created by ytf on 2016/08/10.
 * Description: 基础viewholder
 * Modify by xx on 2016/08/10.
 * Modify detail:
 */
public abstract class BaseViewHolder<T>
{
    protected T data;
    protected View v;
    protected Context mContext;
    protected DefaultImageLoader il;

    public BaseViewHolder(Context context)
    {
        this.mContext = context;
        init(context);
    }

    public BaseViewHolder(T data)
    {
        this.data = data;
        init(mContext);
    }

    private void init(Context context)
    {
        il = new DefaultImageLoader(context);
        int layout = getViewLayout();
        v = View.inflate(context, layout, null);
        operateView(v);
        v.setTag(this);
    }

    /***
     * 将item的视图传入待解析
     *
     * @return item的布局
     */
    protected abstract @LayoutRes int getViewLayout();

    protected abstract void operateView(View v);

    public void setData(T d, int position)
    {
        this.data = d;
        refreshView(position);
    }

    /***
     * @return 返回contentView，即item的布局
     */
    public View getContentView()
    {
        return v;
    }

    /***
     * 数据显示更新调用此方法完成
     */
    protected abstract void refreshView(int position);

    public void showImg(String url, ImageView iv)
    {
        il.showImg(url, iv);
    }
}
